/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.view;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itsc.ds2.bl.DiscBlLocal;
import mx.itsc.ds2.bl.MusicalGenreBlLocal;
import mx.itsc.ds2.model.Discs;
import mx.itsc.ds2.model.MusicalGenres;

/**
 *
 * @author Eli
 */
@ManagedBean
@SessionScoped
public class DiscsBean {

    @EJB
    private MusicalGenreBlLocal musicalGenreBl;
    @EJB
    private DiscBlLocal discBl;
    /**
     * Creates a new instance of DiscsBean
     */
    private List<MusicalGenres> listMusicalGenres;
    private List<Discs> listDiscs;
    private Discs disc;
    private MusicalGenres mg;
    private String accion;
    private int selectedDisc;
    private int selectedMusicalGenre;

    //------------------------------------------------------------------------
    public DiscsBean() {
        disc = new Discs();
    }

    public List<MusicalGenres> getListMusicalGenres() {
        return listMusicalGenres;
    }

    public List<Discs> getListDiscs() {
        listDiscs = discBl.getAll();
        return listDiscs;
    }

    public Discs getDisc() {
        return disc;
    }

    public void setDisc(Discs disc) {
        this.disc = disc;
    }

    public int getSelectedDisc() {
        return selectedDisc;
    }

    public void setSelectedDisc(int selectedDisc) {
        this.selectedDisc = selectedDisc;
    }

    public int getSelectedMusicalGenre() {
        return selectedMusicalGenre;
    }

    public void setSelectedMusicalGenre(int selectedMusicalGenre) {
        this.selectedMusicalGenre = selectedMusicalGenre;
    }

    //-------------------------------------------------------------------------
    public String procesarSeleccion() {
        listMusicalGenres = new ArrayList<MusicalGenres>();
        disc.setIdgenre(musicalGenreBl.findById(selectedMusicalGenre));
        return "ConfirmDiscs";
    }

    public String processAction() {
        disc.setIdgenre(musicalGenreBl.findById(selectedMusicalGenre));
        return "ConfirmDiscs";
    }

    public String procesarAction() {
        if (accion.equalsIgnoreCase("Nuevo")) {
            discBl.register(disc);
        } else {
            if (accion.equalsIgnoreCase("Editar")) {
                discBl.modify(disc);
            } else {
                if (accion.equalsIgnoreCase("Eliminar")) {
                    discBl.delete(disc);
                }
            }
        }

        accion = "";
        return "DiscsList";
    }

    public String prepararCancelar() {
        String regresa = "";
        if (accion.equalsIgnoreCase("Eliminar")) {
            regresa = "DiscsList";

        }
        if (accion.equalsIgnoreCase("Editar") || accion.equalsIgnoreCase("Nuevo")) {
            regresa = "CreateEditDiscs";
        }
        return regresa;
    }

    //listeners-----------------------------------------------------------------------
    public void prepararNuevo(ActionEvent e) {
        listMusicalGenres = new ArrayList<MusicalGenres>();
        accion = "Nuevo";
        disc = new Discs();
    }

    public void prepararEditar(ActionEvent e) {
        accion = "Editar";
        int index = Integer.parseInt(e.getComponent().getClientId().split(":")[2]);
        disc = listDiscs.get(index);
        listMusicalGenres = musicalGenreBl.getListByDisc(disc);
    }

    public void prepararEliminar(ActionEvent e) {
        accion = "Eliminar";
        int index = Integer.parseInt(e.getComponent().getClientId().split(":")[2]);
        disc = listDiscs.get(index);

    }

    public void agregarGenero(ActionEvent evt) {
        if (!listMusicalGenres.contains(musicalGenreBl.findById(selectedMusicalGenre))) {
            listMusicalGenres.add(musicalGenreBl.findById(selectedMusicalGenre));
        } else {
        }
    }

    public void eliminarGeneroDeLista(ActionEvent evt) {//createedit
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        listMusicalGenres.remove(index);

    }
}
